//
//  AskLeaveVC.m
//  SchoolTask
//
//  Created by tusm on 16/8/25.
//  Copyright © 2016年 sm. All rights reserved.
//

#import "AskLeaveVC.h"
#import "AskLeave.h"
#import "AskLeaveCell.h"
#import "MyLeaveVC.h"
#import "LeaveModel.h"

@interface AskLeaveVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,retain) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray * dataArray;

@property (nonatomic,retain) NSString *urlStr;
@property (nonatomic,retain) NSDictionary *urlParameters;
@end

@implementation AskLeaveVC

//请求数据
- (void)fecthDataFromNet:(NSString *)url parameters:(NSDictionary *)parameter {
    
    NSLog(@"%@",url);
    NSLog(@"%@",parameter);
    //初始化请求工具
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        
        NSLog(@"AskLeaveVC-%@",dic);
        NSLog(@"message == %@",dic[@"rows"]);
        
        NSMutableArray * ListArray = dic[@"rows"];
        
        for (NSDictionary * dicList in ListArray)
        {
            LeaveModel * leaveM = [[LeaveModel alloc]init];
            [leaveM setValuesForKeysWithDictionary:dicList];
            [self.dataArray addObject:leaveM];
            NSLog(@"~!~!~!~!==%ld",self.dataArray.count);
        }
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"AskLeaveVC--%@",error);
        
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    _urlStr = @"http://www.100xsys.com.cn/school/app/leaveInfo/listJson";
    _urlParameters = [[NSDictionary alloc]initWithObjectsAndKeys:@"1",@"appWebConfigId",@"1",@"page",@"10",@"rows",@"xuesheng1",@"leaveInfo.loginName",@"1",@"appWebConfigId", nil];
    [self fecthDataFromNet:_urlStr parameters:_urlParameters];

    self.dataArray = [[NSMutableArray alloc]init];
    
    CGRect rect = [UIScreen mainScreen].bounds;
    AskLeave *view = [[AskLeave alloc]init];
    [self.view addSubview:view];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 150, rect.size.width, 500)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
   [_tableView registerNib:[UINib nibWithNibName:@"AskLeaveCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataArray.count;
//    return 10;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AskLeaveCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
//    [cell setLabel];
    
    LeaveModel * model = self.dataArray[indexPath.row];
    [cell setLabel:model.type timeForLeave:[NSString stringWithFormat:@"%@,%@",model.startTime,model.endTime] resultForLeave:[NSString stringWithFormat:@"%@",model.status]
      dateForLeave:[NSString stringWithFormat:@"%@+%@",model.startTime,model.endTime]];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
